package cn.tudo.egweibo2.controller;

import cn.tudo.egweibo2.mapper.WeiboMapper;
import cn.tudo.egweibo2.pojo.dto.WeiboDTO;
import cn.tudo.egweibo2.pojo.entity.Weibo;
import cn.tudo.egweibo2.pojo.vo.UserVO;
import cn.tudo.egweibo2.pojo.vo.WeiboDetailVO;
import cn.tudo.egweibo2.pojo.vo.WeiboIndexVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo/")
public class WeiboContoroller {
    @Autowired
    private WeiboMapper weiboMapper;
    //1.发布微博功能
    @PostMapping("insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        UserVO userVO =(UserVO) session.getAttribute("user");
        if (userVO==null){
            return  2;
        }
        Weibo weibo=new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weibo);
        return 1;

    }
    //2.微博首页列表功能
    @GetMapping("selectIndex")
    public List<WeiboIndexVO>selectIndex(){
        return  weiboMapper.selectIndex();
    }

    //3.微博详情页功能
    @GetMapping("selectById")
    public WeiboDetailVO selectById(int id){
        return weiboMapper.selectById(id);
    }



}
